//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "ddk.h"
#include "Jffs2DirEnumerator.h"
#define _NO_CJFFS2DIRSTRENUMERATOR_CLASSOBJECT_
#include "_CJffs2DirStrEnumerator.cpp"

#include <string.h>
#include <stdlib.h>
#include "jffs2_misc.h"

ECode CJffs2DirStrEnumerator::Current(
                /* [out] */ WStringBuf * pPsa)
{
    wchar_t temp[NAME_MAX_LEN + 1];
    if (m_current[0] == '\0') {
        return E_INVALID_OPERATION;
    }

    if (pPsa == NULL) {
        return E_INVALID_ARGUMENT;
    }
    if (!pPsa->GetCapacity() || pPsa->GetCapacity() < (int)strlen(m_current)) {
        return ErrorToECODE(EINVAL, "CJffs2DirStrEnumerator::Current");
    }

    MbsToWcs(temp, m_current, strlen(m_current) + 1);
    pPsa->Copy(temp);

    return NOERROR;
}

ECode CJffs2DirStrEnumerator::MoveNext()
{
    m_current[0] = '\0';
    struct ELA_IOVEC_TAG iovec1;
    struct ELA_UIO_TAG ioTag;
    int err;

    iovec1.iov_base = m_current;
    iovec1.iov_len = NAME_MAX_LEN + 1;

    ioTag.uio_iov = &iovec1;
    ioTag.uio_iovcnt = 1;

    err = IoOperator::jffs2_fo_dirread(&m_pBasicDirectory->m_file, &ioTag);
    if (err != ENOERR)
        return ErrorToECODE(err, "CJffs2DirStrEnumerator::MoveNext");

    return NOERROR;
}

ECode CJffs2DirStrEnumerator::Reset()
{
    IoOperator::jffs2_fo_dirlseek(&m_pBasicDirectory->m_file, NULL, SEEK_SET);
    m_current[0] = '\0';

    return NOERROR;
}
